package com.edu.buservice.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

/**
 * @program: KQAIA
 * @description:
 * @author: 无恙
 * @create: 2025-04-13 18:58
 **/


public class TenantIdGeneratorUtil {
	private static final String LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	private static final String DIGITS = "0123456789";
	private static final String ALL_CHARS = LETTERS + DIGITS;
	private static final Random random = new Random();

	public static String generateTenantId() {
		// 随机确定长度（6、7、8）
		int[] possibleLengths = {6, 7, 8};
		int length = possibleLengths[random.nextInt(possibleLengths.length)];

		// 存储字符的集合
		List<Character> chars = new ArrayList<>();

		// 确保至少包含一个字母和一个数字
		chars.add(LETTERS.charAt(random.nextInt(LETTERS.length())));
		chars.add(DIGITS.charAt(random.nextInt(DIGITS.length())));

		// 生成剩余字符
		for (int i = 0; i < length - 2; i++) {
			chars.add(ALL_CHARS.charAt(random.nextInt(ALL_CHARS.length())));
		}

		// 打乱顺序避免固定模式
		Collections.shuffle(chars);

		// 转换为字符串
		StringBuilder sb = new StringBuilder();
		for (char c : chars) {
			sb.append(c);
		}
		return sb.toString();
	}
}
